package gov.niis.service.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Dbconn {
	
	public static Connection getConnection(){
		
		Connection conn = null;		
		try {
			
			String DB_DRV      = "oracle.jdbc.driver.OracleDriver";
			String DB_IP       = "192.168.100.60";  
			String DB_PORT     = "1521";       
			String DB_SID      = "sim";  
			String DB_TYPE     = "oracle";
			String DB_ID       = "bizaid";        
			String DB_PWD      = "qhdks0109";  
			
			String DB_URL    = "jdbc:"+DB_TYPE+":thin:@"+DB_IP+":"+DB_PORT+":"+DB_SID;
			Class.forName(DB_DRV);
			conn = DriverManager.getConnection(DB_URL, DB_ID, DB_PWD);
	      
	        System.out.println("Connect Success");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return conn;
	
	}	
	
	
	public static void releaseConnection(ResultSet rs, Statement stmt, Connection conn) {
		if (rs != null) {
			try {
					rs.close();
					rs = null;
			} catch (SQLException sqle) {
				sqle.printStackTrace();
			}
		}

		if (stmt != null) {
			try {
				stmt.close();
				stmt = null;
			} catch (SQLException sqle) {
				sqle.printStackTrace();
			}
		}

		if (conn != null) {
			try {
				conn.setAutoCommit(true);
				conn.close();
			} catch (SQLException sqle) {
				sqle.printStackTrace();
			} finally {
				conn = null;
			}
		}
	}

	public static void releaseConnection(ResultSet rs, PreparedStatement pstmt, Connection conn) {
		if (rs != null) {
			try {
				rs.close();
				rs = null;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		if (pstmt != null) {
			try {
				pstmt.close();
				pstmt = null;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		if (conn != null) {
			try {
				conn.setAutoCommit(true);
				conn.close();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				conn = null;
			}
		}
	}


	public static void releasePreparedStatement(PreparedStatement pstmt) {
		if (pstmt != null) {
			try {
				pstmt.close();
				pstmt = null;
			} catch (SQLException sqle) {
				sqle.printStackTrace();
			}
		}
	}


	public static void releaseConnection(Connection conn) {
		if (conn != null) {
			try {
				conn.setAutoCommit(true);
				conn.close();
			} catch (SQLException sqle) {
				sqle.printStackTrace();
			} finally {
				conn = null;
			}
		}
	}
}